//
//  MLPauseLayer.m
//  WaterProof
//
//  Created by victor0321 on 12/1/6.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "MLPauseLayer.h"
#import "SimpleAudioEngine.h"

@interface MLPauseLayer (PrivateMethods)
-(void) resumeItemTouched:(id)sender;
-(void) mainMenuItemTouched:(id)sender;
-(void) stageItemTouched:(id)sender;
-(void) helpItemTouched:(id)sender;
@end

@implementation MLPauseLayer

+(id) sceneWithColor:(ccColor4B)color {
    CCLOG(@"===========================================");
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
    return [[[self alloc] initColor:color] autorelease];
}

-(id) initColor:(ccColor4B)color
{
    if ((self = [super initWithColor:color]))
    {
        screenSize = [[CCDirector sharedDirector]winSize];
        self.isTouchEnabled=YES;
        
        CCSprite* resume = [CCSprite spriteWithFile:@"btn_pause_resume.png"];
        CCSprite* resumeSelected = [CCSprite spriteWithFile:@"btn_pause_resume.png"];
        resumeSelected.color = ccGRAY;
        CCMenuItemSprite* resumeItem = [CCMenuItemSprite itemFromNormalSprite:resume selectedSprite:resumeSelected target:self selector:@selector(resumeItemTouched:)];
        
        CCSprite* mainMenu = [CCSprite spriteWithFile:@"btn_pause_mainmenu.png"];
        CCSprite* mainMenuSelected = [CCSprite spriteWithFile:@"btn_pause_mainmenu.png"];
        mainMenuSelected.color = ccGRAY;
        CCMenuItemSprite* mainMenuItem = [CCMenuItemSprite itemFromNormalSprite:mainMenu selectedSprite:mainMenuSelected target:self selector:@selector(mainMenuItemTouched:)];
        
        CCSprite* stage = [CCSprite spriteWithFile:@"btn_pause_stage.png"];
        CCSprite* stageSelected = [CCSprite spriteWithFile:@"btn_pause_stage.png"];
        stageSelected.color = ccGRAY;
        CCMenuItemSprite* stageItem = [CCMenuItemSprite itemFromNormalSprite:stage selectedSprite:stageSelected target:self selector:@selector(stageItemTouched:)];
        
        CCSprite* help = [CCSprite spriteWithFile:@"btn_pause_help.png"];
        CCSprite* helpSelected = [CCSprite spriteWithFile:@"btn_pause_help.png"];
        helpSelected.color = ccGRAY;
        CCMenuItemSprite* helpItem = [CCMenuItemSprite itemFromNormalSprite:help selectedSprite:helpSelected target:self selector:@selector(helpItemTouched:)];
        
        CCMenu* menu = [CCMenu menuWithItems:resumeItem, mainMenuItem, stageItem, helpItem, nil];
        [menu setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
        [menu alignItemsVerticallyWithPadding:20];
        
        //[menu alignItemsInGridWithPadding:CGPointMake(20, 20) columns:2];
        
        [self addChild:menu];
        
        //CGFloat movDist = (base.contentSize.height+screenSize.height)/2;
        //CCMoveBy* move = [CCMoveBy actionWithDuration:1 position:CGPointMake(0, -movDist)];
        //CCEaseBackOut* ease = [CCEaseBackOut actionWithAction:move];
        //CCEaseElasticOut* ease = [CCEaseElasticOut actionWithAction:move period:0.8];
        
        //[menu runAction:ease];
        //[self addChild:menu z:2];
        //[[MLGameLayer sharedLayer] unschedule:@selector(tick:)];
    }
    return self;
}

-(void) resumeItemTouched:(id)sender{
    
   /* CCNode *node = [self getChildByTag:BaseTag];
    CCSprite *base = (CCSprite*)node;
    CGFloat movDist = (base.contentSize.height+screenSize.height)/2;
    CCMoveBy* move = [CCMoveBy actionWithDuration:1 position:CGPointMake(0, movDist)];
    CCEaseBackInOut* ease = [CCEaseBackInOut actionWithAction:move];
    CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onCallFunc)];
    CCSequence* sequence = [CCSequence actions:ease, func, nil];
    [base runAction:sequence];*/
    [self.parent removeChild:self cleanup:YES];
    [[SimpleAudioEngine sharedEngine] playEffect:@"drop.caf"];
    
}

-(void) mainMenuItemTouched:(id)sender{
    [[MLGameLayer sharedLayer] transitionToMenu];
    [[SimpleAudioEngine sharedEngine] playEffect:@"drop.caf"];
}

-(void) stageItemTouched:(id)sender{
    [[SimpleAudioEngine sharedEngine] playEffect:@"drop.caf"];

}

-(void) helpItemTouched:(id)sender{
    [[SimpleAudioEngine sharedEngine] playEffect:@"drop.caf"];
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}

/*-(void) onCallFunc
{
	[self.parent removeChild:self cleanup:YES];
}*/

/*-(BOOL) isTouchFor:(CGPoint)touchPoint Tag:(UILayerTags)tag {
    
    CCNode* node = [uiLayer getChildByTag:tag];
     // center of the node, since it is the child of uiLayer not scene,
     // it's coordinate is compare to (0,0) or the origin of uiLayer (also is (0,0))
     // so we have to manual add the uiLayer's position
     CGPoint position = [node position];
     position.x += [uiLayer position].x - [uiLayer contentSize].width/2;
     position.y += [uiLayer position].y - [uiLayer contentSize].height/2;
     CGSize size = [node contentSize];
     CGRect rect = CGRectMake(position.x-size.width/2, position.y-size.height/2, size.width, size.height);
     return CGRectContainsPoint(rect, touchPoint);
    CCNode* node = [self getChildByTag:tag];
    return CGRectContainsPoint([node boundingBox], touchPoint);
}*/

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
    /*CGPoint location = [touch locationInView: [touch view]];
	location =  [[CCDirector sharedDirector] convertToGL:location];
    
    if (CGRectContainsPoint([paused boundingBox], location)) {
        [self.parent removeChild:self cleanup:YES];
    }*/
    return YES;
}

- (void) dealloc
{
    CCLOG(@"===========================================");
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

    [super dealloc];
}


@end
